package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.CollectCommodityAdapter;
import com.luck.easyfoodbatch.mvp.mine.CollectListModel;
import com.luck.easyfoodbatch.mvp.mine.CollectListPresenter;
import com.luck.easyfoodbatch.mvp.mine.CollectListView;
import com.luck.easyfoodbatch.mvp.model.ReturnCollect;
import com.luck.easyfoodbatch.ui.ShopDetailsActivity;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.fragment
 * email：893855882@qq.com
 * data：16/9/27
 */
public class CommodityFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener, CollectListView {
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private View rootView;
    private CollectCommodityAdapter adapter;
    private List<ReturnCollect.DataBean.ModelsBean> list = new ArrayList<>();
    private CollectListModel listModel;
    private int totalPage = 1;
    private int page = 1;
    private int isRefresh = 0;
    private String p_id = "3";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_commodity, container, false);
            ButterKnife.bind(this, rootView);
            initViews();
        }

        return rootView;
    }

    private void initViews() {
        p_id = (String) SharedPreferencesUtil.getData(getActivity(), HttpUtil.P_ID, "3");
        listModel = new CollectListPresenter(this, mContext);
        showProgressDialog(getString(R.string.app_loading));
        listModel.collectListData(page, 1, p_id);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(manager);
        adapter = new CollectCommodityAdapter(mContext);
        recyclerView.setAdapterWithProgress(adapter);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                ReturnCollect.DataBean.ModelsBean bean = list.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("gid", bean.getGoods().getGid() + "");
                startAct(ShopDetailsActivity.class, bundle);
            }
        });
        adapter.setError(R.layout.view_error).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.resumeMore();
            }
        });

        adapter.addAll(list);

        recyclerView.setAdapter(adapter);
        recyclerView.setRefreshListener(this);
    }

    @Override
    public void onLoadMore() {
        //刷新
        if( !isFastDoubleClick()) {
            isRefresh = 1;
            page++;
            if (page > totalPage) {
                adapter.stopMore();
                return;
            }
            listModel.collectListData(page, 1, p_id);
        }else {
            adapter.stopMore();
        }
    }

    @Override
    public void onRefresh() {
        if (!isFastDoubleClick()) {
            isRefresh = 0;
            page = 1;
            listModel.collectListData(page, 1, p_id);
        }else {
            adapter.stopMore();
        }
    }


    @Override
    public void collectSuccess(ReturnCollect model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                ReturnCollect.DataBean data = model.getData();
                totalPage = data.getTotalPage();
                // 下拉刷新 清空集合
                switch (isRefresh) {
                    case 0:
                        // 下拉刷新
                        list.clear();
                        list = data.getModels();
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        break;
                    case 1:
                        // 上拉加载
                        list.addAll(data.getModels());
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        break;
                }
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {
        //T.showToast(mContext, getString(R.string.app_http_error));
    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
